//
//  CJIntroductionCollectionViewCell.m
//  CoJellyCartoon
//
//  Created by dllo on 16/4/6.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "CJIntroductionCollectionViewCell.h"
#import "CJIntroductionHeader.h"
#import "CJRelateVideTableViewCell.h"
#import "CJRelatesModel.h"
@interface CJIntroductionCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *IntroductionTableView;
@property (nonatomic, strong) CJIntroductionHeader *IntroductionHeader;

@end
@implementation CJIntroductionCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatTableView];
    }
    return self;
}
-(void)creatTableView{
    
    self.IntroductionHeader = [[CJIntroductionHeader alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 50)];
    
    self.IntroductionTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.IntroductionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.IntroductionTableView.dataSource = self;
    
    self.IntroductionTableView.delegate = self;
    
    self.IntroductionTableView.bounces = YES;
    
    self.IntroductionTableView.scrollEnabled = YES;
    
    self.IntroductionTableView.tableHeaderView = self.IntroductionHeader;
    
    [self.IntroductionTableView registerClass:[CJRelateVideTableViewCell class] forCellReuseIdentifier:@"CJRelateVideTableViewCell"];
    
    [self addSubview:self.IntroductionTableView];
    
}

-(void)layoutSubviews{

    self.IntroductionTableView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 94);

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.preplayPageModel.relates.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CJRelateVideTableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"CJRelateVideTableViewCell"];
    CJRelatesModel *relateModel = self.preplayPageModel.relates[indexPath.row];
    cell.relateModel = relateModel;
    
    return cell;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"相关视频";
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.bounds.size.height / 6.5;
}

-(void)setPreplayPageModel:(CJPrePlayPageVIewModel *)preplayPageModel{
    _preplayPageModel = preplayPageModel;
    
    self.IntroductionHeader.title.text = preplayPageModel.title;
    self.IntroductionHeader.descriptionLabel.text = preplayPageModel.desc;
    
    self.IntroductionHeader.playCount.text = [NSString stringWithFormat:@"%@",preplayPageModel.stat[@"view"]];
    self.IntroductionHeader.commentsCount.text = [NSString stringWithFormat:@"%@",preplayPageModel.stat[@"danmaku"]];
    
    [self.IntroductionTableView reloadData];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CJRelatesModel *relateModel = self.preplayPageModel.relates[indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"relateInfo" object:nil userInfo:@{@"relateInfo": relateModel}];
}
@end
